package com.common.www.controller.programme;

import com.alibaba.fastjson.JSON;
import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.programme.ProjectScheme;
import com.common.www.bean.entity.project.Project;
import com.common.www.controller.base.DefaultCrudController;
import com.common.www.data.NetCode;
import com.common.www.service.programme.ContrastSchemeService;
import com.common.www.service.programme.ProjectSchemeService;
import com.common.www.service.programme.SupplyCompanySchemeService;
import com.common.www.service.project.ProjectService;
import com.common.www.service.project.BiddingCompanyService;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;
import com.common.www.utils.office.ImportExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@CrossOrigin
@RestController
@RequestMapping(value = {"mgt/projectScheme"})
@Api(tags = "MGT 方案管理接口")
public class MGTProjectSchemeController extends DefaultCrudController<ProjectScheme, Long> {
    @Autowired
    private ContrastSchemeService contrastSchemeService;
    @Autowired
    private ProjectSchemeService projectSchemeService;
    @Autowired
    private SupplyCompanySchemeService supplyCompanySchemeService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private BiddingCompanyService biddingCompanyService;

    protected IBaseService getService() {
        return projectSchemeService;
    }

    @ApiOperation("查询上传的调研项目方案列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/getProjectScheme", method = RequestMethod.POST)
    public Result getProjectScheme(Long projectId) {
        if (ParamsUtils.isInputEmpty(projectId)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        HashMap hashMap = new HashMap();
        hashMap.put("project.id",projectId);
        List<ProjectScheme> projectSchemeList = projectSchemeService.queryToList(hashMap,null);

        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, List<ProjectScheme>> nameList = projectSchemeList.stream().collect(Collectors.groupingBy(ProjectScheme :: getAssignProjectName));
        for (Map.Entry<String, List<ProjectScheme>> item : nameList.entrySet()) {
            List<ProjectScheme> value = item.getValue();

            Map<String, Object> tempMap = new HashMap<>();
            if (value.size() > 0){
                tempMap.put("project",value.get(0).getProject());
                tempMap.put("assignProjectName",value.get(0).getAssignProjectName());
                tempMap.put("uploadTime",value.get(0).getCreateTime());
                tempMap.put("fileName",value.get(0).getFileName());
            }
            mapList.add(tempMap);
        }
        return R.ok(mapList);
    }


    @ApiOperation("某个调研项目的方案详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/getProjectSchemeDetailList", method = RequestMethod.POST)
    public Result getProjectSchemeDetailList(Long projectId,String projectScheme) {
        if (ParamsUtils.isInputEmpty(projectId, projectScheme)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        HashMap hashMap = new HashMap();
        hashMap.put("project.id",projectId);
        hashMap.put("projectScheme",projectScheme);
        List<ProjectScheme> projectSchemeList = projectSchemeService.queryToList(hashMap,null);
        return R.ok(projectSchemeList);
    }


    @ApiOperation("编辑信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJson", method = RequestMethod.POST)
    public Result saveByJson(String json) {
        ProjectScheme projectScheme = JSON.parseObject(json, ProjectScheme.class);
        return R.ok(projectScheme);
    }

    @ApiOperation("按项目和调研项目删除方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "String", defaultValue = ""),
            @ApiImplicitParam(name = "jieDuan", value = "", defaultValue = ""),
    })
    @RequestMapping(value = "/conditionDelete", method = RequestMethod.POST)
    public Result conditionDelete(Long projectId,String assignProjectName) {
        if (ParamsUtils.isInputEmpty(projectId,assignProjectName)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        projectSchemeService.conditionDelete(projectId,assignProjectName);
        return R.ok();
    }



    @ApiOperation(value = "导入调研项目方案", nickname = "", notes = "")
    @ApiImplicitParams(value = {
    })
    @RequestMapping(value = "/importProjectScheme", produces = {"application/json"}, method = RequestMethod.POST)
    public Result importProjectScheme(MultipartFile file, Long projectId) throws Exception {
        Project project = projectService.findOne(projectId);
        InputStream inputStream = file.getInputStream();
        String fileName = file.getOriginalFilename();
        Workbook workbook = new ImportExcelUtil().getWorkbook(inputStream, fileName);
        List<ProjectScheme> listImport = projectSchemeService.importSimulate(workbook,project,fileName);
        //Excel表格存在合并项，整理listImport的数据格式后再保存
        List<ProjectScheme> listArrange = new ArrayList<>();
        String category = null;
        String minorItems = null;
        for (ProjectScheme scheme : listImport){
            String tempCategory = scheme.getCategory();
            String tempMinorItems = scheme.getMinorItems();
            if (StringUtils.isNotEmpty(tempCategory)){
                category = tempCategory;
            }else {
                if (StringUtils.isNotEmpty(category)){
                    scheme.setCategory(category);
                }
            }
            if (StringUtils.isNotEmpty(tempMinorItems)){
                minorItems = tempMinorItems;
            }else {
                if (StringUtils.isNotEmpty(minorItems)){
                    scheme.setMinorItems(minorItems);
                }
            }
            listArrange.add(scheme);
        }
        // 先删除原来的
        projectSchemeService.conditionDelete(projectId,listArrange.get(0).getAssignProjectName());
        projectSchemeService.save(listArrange);
        return R.ok();
    }




}

